<template>
  <div class="big-box">
    <el-dialog
      title="新增车辆"
      :visible="isShow"
      width="30%"
      @close="close"
    >
      <el-form ref="CarList" label-width="80px" :model="CarList" :rules="CarListRules">
        <el-form-item label="车辆类型:" prop="truckTypeId">
          <el-select v-model="CarList.truckTypeId" placeholder="请选择">
            <el-option
              v-for="item in list"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="车牌号码:" prop="licensePlate">
          <el-input v-model="CarList.licensePlate" />
        </el-form-item>
        <el-form-item label="GPS设备ID:" class="GPS" prop="deviceGpsId">
          <el-input v-model="CarList.deviceGpsId" />
        </el-form-item>
        <el-form-item class="Form-item">
          <el-button type="primary" @click="submit">确定</el-button>
          <el-button @click="close">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { addCar, getCarList } from '@/api/truck'
export default {
  props: {
    isShow: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      list: [], // 下拉车辆类型列表
      CarList: {
        truckTypeId: '', // 车辆类型Id
        licensePlate: '', // 车牌号码
        deviceGpsId: '' // GPS设备id
      },
      CarListRules: {
        truckTypeId: [
          { required: true, message: 'GPS设备ID不能为空', trigger: 'blur' }
        ],
        licensePlate: [
          { required: true, message: '车辆类型不能为空', trigger: 'blur' }
        ],
        deviceGpsId: [
          { required: true, message: '车牌号码不能为空', trigger: 'blur' }
        ]

      }
    }
  },
  async created() {
    // 获取车型列表
    const res = await getCarList()
    this.list = res.data
  },
  methods: {
    // 点击取消--x号隐藏消失
    close() {
      this.$emit('update:isShow', false)
      this.$refs.CarList.resetFields()// 清理表单
    },
    // 点击确定按钮
    async submit() {
      await this.$refs.CarList.validate()
      await addCar(this.CarList)
      this.$message.success('添加成功')
      this.close()// 清空表单
      this.$emit('update')// 通知父组件刷新页面
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog__header{
  background-color: #f3f4f7;
}

::v-deep .el-dialog{
  height: 450px;
}
::v-deep .is-required{
    margin-top: 25px;
}
::v-deep .el-form{
  flex-wrap: wrap;
}
.Form-item{
  display: flex;
  justify-content: center;
  .el-button{
    width: 90px;
  }
}
::v-deep .el-input--suffix{
width: 456px;
}
::v-deep .el-form-item__label{
padding: 0px;
text-align: none;
width: 86px !important;
}
::v-deep .el-form-item__content{
margin-left: 100px !important;
}
::v-deep .el-icon-arrow-up{
margin-right: 42px !important;
}

</style>
